package com.ncu.greendao;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.ncu.greendao.db.DaoMaster;
import com.ncu.greendao.db.DaoSession;
import com.ncu.greendao.db.UserDao;

import pl.com.salsoft.sqlitestudioremote.SQLiteStudioService;

public class MainActivity extends AppCompatActivity {
    public static final String TAG="RightFragment";
    UserDaoUtil userDaoUtil;
    User user=null;
    private DaoManager mManager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        SQLiteStudioService.instance().start(this);
        userDaoUtil = new UserDaoUtil(this);
        Button button = (Button)findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //创建数据库成功了

            }
        });
        user = new User();//实例化
        Button button2 = (Button)findViewById(R.id.button2);
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //由于自动增加所以其实可以不用传入id值
                user.setId(null);
                user.setAge(18);
                user.setName("xiaohong");
                userDaoUtil.insertUser(user);
            }
        });
        Button button3 = (Button)findViewById(R.id.button3);
        button3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //删除全部的
                userDaoUtil.deleteAll();
            }
        });
        Button button4= (Button)findViewById(R.id.button4);
        button4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                user.setId((long) 1);
                user.setAge(19);
                user.setName("xiaobai");
                userDaoUtil.updateUser(user);
            }
        });
        Button button5= (Button)findViewById(R.id.button5);
        button5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                user = userDaoUtil.queryUserById(1);
                Log.d(TAG,user.getName()+user.getAge());
            }
        });
    }
}
